<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "mkt_collect".
 *
 * @property string $id
 * @property string $user_id
 * @property string $post_id
 * @property string $create_time
 */
class MktCollect extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'mkt_collect';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['user_id', 'post_id'], 'integer'],
            [['create_time'], 'safe'],
            [['user_id', 'post_id'], 'unique', 'targetAttribute' => ['user_id', 'post_id'], 'message' => 'The combination of 我 and 我收藏的贴子 has already been taken.']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'user_id' => '我',
            'post_id' => '我收藏的贴子',
            'create_time' => '时间',
        ];
    }

    /**
     * @inheritdoc
     * @return MktCollectQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new MktCollectQuery(get_called_class());
    }

    public function getUser()
    {
        return $this->hasOne(MktUser::className(), ['id' => 'user_id']);
    }

    public function getPost()
    {
        return $this->hasOne(MktPost::className(), ['id' => 'post_id']);
    }

    public static function deleteCollectAjax($params)
    {
        $model = MktCollect::findOne($params['id']);
        if (null === $model) {
            return \yii\helpers\Json::encode(['code' => 0, 'data' => 'OK']);
        }
        $model->delete();
        return \yii\helpers\Json::encode(['code' => 0, 'data' => 'OK']);
    }

}
